//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexCodes: ["男", "女"],
    sexCodeIndex: 0,
    linkmanName: '',
    linkmanHeadUrl: app.globalData.headerUrl,
    linkmanSex: '男',
    linkmanAddress: '',
    linkmanTelephone: '',
    linkmanCompany: '',
    linkmanQQ: '',
    linkmanEmail: '',
    studentID: '',

    linkmanGuid: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      linkmanGuid: options.linkmanGuid
    });
    console.log(this.data.linkmanGuid);
    var that = this
    wx.showNavigationBarLoading();
    getLinkDetil(that);
  },
  clearInput() {
    this.setData({
      sexCodeIndex: 0,
      linkmanName: '',
      linkmanHeadUrl: '',
      linkmanSex: '男',
      linkmanAddress: '',
      linkmanTelephone: '',
      linkmanCompany: '',
      linkmanQQ: '',
      linkmanEmail: '',
      studentID: '',
    });
  },
  submit() {
    var that = this
    wx.showNavigationBarLoading();
    saveLinkman(that);
  },
  bindsexChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      sexCodeIndex: e.detail.value,
      linkmanSex:this.data.sexCodes[e.detail.value]
    })
  },
  bindlinkmanNameInput: function (e) {
    this.setData({
      linkmanName: e.detail.value
    })
  },
  bindlinkmanAddressInput: function (e) {
    this.setData({
      linkmanAddress: e.detail.value
    })
  },
  bindlinkmanTelephoneInput: function (e) {
    this.setData({
      linkmanTelephone: e.detail.value
    })
  },
  bindlinkmanCompanyInput: function (e) {
    this.setData({
      linkmanCompany: e.detail.value
    })
  },
  bindlinkmanQQInput: function (e) {
    this.setData({
      linkmanQQ: e.detail.value
    })
  },
  bindlinkmanEmailInput: function (e) {
    this.setData({
      linkmanEmail: e.detail.value
    })
  },
  bindlinkmanQQInput: function (e) {
    this.setData({
      linkmanQQ: e.detail.value
    })
  },
  bindstudentIDInput: function (e) {
    this.setData({
      studentID: e.detail.value
    })
  },
})
var getLinkDetil = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/getLinkmanByGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuid: self.data.linkmanGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.hideNavigationBarLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        self.setData({
          linkmanName: res.data.UserArea.linkmanName,
          // linkmanSex: res.data.UserArea.linkmanSex,
          linkmanAddress: res.data.UserArea.linkmanAddress,
          linkmanTelephone: res.data.UserArea.linkmanTelephone,
          linkmanCompany: res.data.UserArea.linkmanCompany,
          linkmanQQ: res.data.UserArea.linkmanQQ,
          linkmanEmail: res.data.UserArea.linkmanEmail,
          studentID: res.data.UserArea.studentID,
        });
        if (res.data.UserArea.linkmanHeadUrl != ''){
          self.setData({
            linkmanHeadUrl: res.data.UserArea.linkmanHeadUrl
          });
        }
        if (res.data.UserArea.linkmanSex == '男'){
          self.setData({
            sexCodeIndex: 0,
            linkmanSex: self.data.sexCodes[0]
          })
        }else{
          self.setData({
            sexCodeIndex: 1,
            linkmanSex: self.data.sexCodes[1]
          })
        }
        
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}
var saveLinkman = function (self) {
  if (self.data.linkmanName == '') {
    wx.showToast({
      title: '请输入姓名！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else if (self.data.linkmanTelephone == '') {
    wx.showToast({
      title: '请输入手机号码！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else if (self.data.studentID == '') {
    wx.showToast({
      title: '请输入学号！',
      image: '../../images/img_jinggao.png',
      duration: 1000
    })
  } else {
    wx.request({
      url: app.globalData.ServiceUrl + 'Linkman/updateLinkman', //接口地址
      method: 'POST',
      data: {
        ValidateData: app.globalData.ValidateData,
        paras: {
          linkmanName: self.data.linkmanName,
          linkmanHeadUrl: self.data.linkmanHeadUrl,
          linkmanSex: self.data.linkmanSex,
          linkmanAddress: self.data.linkmanAddress,
          linkmanTelephone: self.data.linkmanTelephone,
          linkmanCompany: self.data.linkmanCompany,
          linkmanQQ: self.data.linkmanQQ,
          studentID: self.data.studentID,
          linkmanEmail: self.data.linkmanEmail,
          linkmanGuid: self.data.linkmanGuid,
        }
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideNavigationBarLoading();
        if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
          wx.showToast({
            title: '联系人修改成功',
            icon: 'success',
            duration: 2000,
            complete: function () {
              //上传成功，关闭页面
              wx.navigateBack({})
            } //接口调用结束的回调函数  
          })
        }
      }
    })
  }
}